//
//  PaymentViewController.m
//  ActSistant
//
//  Created by Hieu Nguyen Trung on 2/22/13.
 //

#import "PaymentViewController.h"
#import "OverviewItemCell.h"
#import <QuartzCore/QuartzCore.h>
#import "ActSettingViewController.h"

#define ARRAY_LABEL_EXPENSES @[@"Resume Paper", @"Fitness Dues", @"J.Crew Suit Jacket"]
static int arrayPriceExpenses[] = {25, 68, 350};
static int arrayDaysExpenses[] = {50, 50, 38};
#define NUMBER_EXPENSES_ITEM 3

#define ARRAY_LABEL_PAID @[@"Nixon Watches Spont", @"Modern Family", @"Homeland", @"Homeland", @"Modern Family"]
static int arrayPricePaid[] = {300, 1800, 325, 325, 1800};
static int arrayDaysPaid[] = {30, 38, 50, 50, 38};
#define NUMBER_PAID_ITEM 5

#define ARRAY_LABEL_UNPAID @[@"Homeland", @"Homeland", @"Modern Family"]
static int arrayPriceUnpaid[] = {325, 325, 1800};
static int arrayDaysUnpaid[] = {50, 50, 38};
#define NUMBER_UNPAID_ITEM 3

@interface PaymentViewController ()

@end

@implementation PaymentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initializeView];
    [self initializeData];
    [self updateChange];
//    [self addBottomView];
}
-(IBAction)doSetting:(id)sender{
//    ActSettingViewController *controller = [[ActSettingViewController alloc] initWithNibName:@"ActSettingViewController" bundle:nil];
//    controller.navigationController.navigationBarHidden = NO;
//    controller.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:controller animated:YES];
//    [controller release];
}
-(void)addBottomView{
    CGRect bound = self.view.bounds;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, bound.size.height-40, bound.size.width, 40)];
    UIImageView *bg = [[UIImageView alloc] initWithFrame:view.bounds];
    [bg setImage:[UIImage imageNamed:@"payment_bottom_view_background"]];
    [view addSubview:bg];
    [bg release];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 40)];
    [image setImage:[UIImage imageNamed:@"payment_right_icon"]];
    [view addSubview:image];
    [image release];
    
    if(!_bottomTitle){
        _bottomTitle = [[UILabel alloc] initWithFrame:CGRectMake(150, 0, 150, 40)];
        [_bottomTitle setFont:UIFontSystemBold(22)];
        [_bottomTitle setTextColor:[UIColor whiteColor]];
        [_bottomTitle setBackgroundColor:[UIColor clearColor]];
    }
    [view addSubview:_bottomTitle];
    [self.view addSubview:view];
    [view release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) initializeView{
    [self.navigationView.btnBack setBackgroundImage:[UIImage imageNamed:@"backbar_button_root"] forState:UIControlStateNormal];
    [self.navigationView.btnBack setUserInteractionEnabled:false];
    [self.navigationView.btnRight1 addTarget:self action:@selector(buttonSettingClicked) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(33, 0)];
    [self.searchBar setBackgroundImage:[UIImage imageNamed:@"SearchBarBackground.png"]];
    [self.searchBar setSearchFieldBackgroundImage:[[UIImage imageNamed:@"SearchFieldBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 100, 0, 100)] forState:UIControlStateNormal];
    [self.searchBar setFrame:[Util adjustCurrentFrame:self.searchBar.frame withOriginX:0 originY:0 width:0 height:-3]];
    [self.searchBar setDelegate:self];
    
    for (UIView *view in [self.searchBar subviews]){
        if ([view isKindOfClass:[UITextField class]]){
            [((UITextField *)view) setLeftViewMode:UITextFieldViewModeNever];
            [((UITextField *)view) setClearButtonMode:UITextFieldViewModeWhileEditing];
        }
    }
    
    self.navigationController.navigationBarHidden = true;
}

-(void) initializeData{
    currentIndex = indexPaid;
}

-(NSMutableArray *) arrayFromCIntArray:(int[])array size:(int)size{
    NSMutableArray *tempArray = [[[NSMutableArray array] retain] autorelease];
    for (int i = 0; i < size; i++){
        [tempArray addObject:[NSNumber numberWithInt:array[i]]];
    }
    return tempArray;
}

-(void) updateChange{
    [self updateData];
    [self updateViews];
}

-(void) updateViews{
    switch (currentIndex) {
        case indexExpenses:
            [self.buttonExpenses setSelected:true];
            [self.buttonPaid setSelected:false];
            [self.buttonUnpaid setSelected:false];
            [_bottomTitle setText:@"-443"];
            break;
        case indexPaid:
            [self.buttonExpenses setSelected:false];
            [self.buttonPaid setSelected:true];
            [self.buttonUnpaid setSelected:false];
            [_bottomTitle setText:@"+14,568"];
            break;
        case indexUnpaid:
            [self.buttonExpenses setSelected:false];
            [self.buttonPaid setSelected:false];
            [self.buttonUnpaid setSelected:true];
            [_bottomTitle setText:@"-2,450"];
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}

-(void) updateData{
    if (currentIndex == indexPaid){
        self.arrayLabelTitle = [[NSMutableArray arrayWithArray:ARRAY_LABEL_PAID] retain];
        self.arrayDays = [self arrayFromCIntArray:arrayDaysPaid size:NUMBER_PAID_ITEM];
        self.arrayPrice = [self arrayFromCIntArray:arrayPricePaid size:NUMBER_PAID_ITEM];
        numberOfItem = NUMBER_PAID_ITEM;
    }
    else if (currentIndex == indexExpenses){
        self.arrayLabelTitle = [[NSMutableArray arrayWithArray:ARRAY_LABEL_EXPENSES] retain];
        self.arrayDays = [self arrayFromCIntArray:arrayDaysExpenses size:NUMBER_EXPENSES_ITEM];
        self.arrayPrice = [self arrayFromCIntArray:arrayPriceExpenses size:NUMBER_EXPENSES_ITEM];
        numberOfItem = NUMBER_EXPENSES_ITEM;
    }
    else if (currentIndex == indexUnpaid){
        self.arrayLabelTitle = [[NSMutableArray arrayWithArray:ARRAY_LABEL_UNPAID] retain];
        self.arrayDays = [self arrayFromCIntArray:arrayDaysUnpaid size:NUMBER_UNPAID_ITEM];
        self.arrayPrice = [self arrayFromCIntArray:arrayPriceUnpaid size:NUMBER_UNPAID_ITEM];
        numberOfItem = NUMBER_UNPAID_ITEM;
    }
}

- (void)dealloc {
    [_bottomTitle release];
    [_searchBar release];
    [_buttonExpenses release];
    [_buttonPaid release];
    [_buttonUnpaid release];
    [_tableView release];
    [_arrayDays release];
    [_arrayLabelTitle release];
    [_arrayPrice release];
    [_navigationView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setSearchBar:nil];
    [self setButtonExpenses:nil];
    [self setButtonPaid:nil];
    [self setButtonUnpaid:nil];
    [self setTableView:nil];
    [self setArrayDays:nil];
    [self setArrayLabelTitle:nil];
    [self setArrayPrice:nil];
    [self setNavigationView:nil];
    [super viewDidUnload];
}

#pragma mark - UITableView Methods Implementation
-(int) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return numberOfItem;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 51;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OverviewItemCell *cell = nil;
    
    NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"OverviewItemCell" owner:self options:nil];
    for (id currentObject in objectsInXIB){
        if ([currentObject isKindOfClass:[OverviewItemCell class]]){
            cell = currentObject;
            int index = indexPath.row;
            cell.hasProgressView = false;
            [cell.layer setBorderColor:[UIColor lightGrayColor].CGColor];
            [cell.layer setBorderWidth:0.5];
            [cell.labelTitle setText:self.arrayLabelTitle[index]];
            [cell.labelPrice setText:[NSString stringWithFormat:@"$ %d", [self.arrayPrice[index] integerValue]]];
            [cell.labelDays setText:[NSString stringWithFormat:@"%d days ago", [self.arrayDays[index] integerValue]]];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            [cell setClipsToBounds:false];
        }
    }
    return cell;
}

#pragma mark - Actions Handle
- (IBAction)buttonPaymentTabClicked:(UIButton *)sender {
    currentIndex = sender.tag;
    [self updateChange];
}

-(void) buttonSettingClicked{
    
}

#pragma mark - UISearchBarDelegate Methods Implementation
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}
@end
